package base.thread_JUC2.chapter01.demo010;

import java.util.concurrent.TimeUnit;

public class SearchTask implements Runnable{

    private Result result;

    public SearchTask (Result result) {
        this.result = result;
    }

    @Override
    public void run() {
        String name=Thread.currentThread().getName();
        System.out.printf("%s: Start\n",name);
        try {
            // 调用耗时操作
            doTask();
            result.setName(name);//线程执行结果封装进Result
        } catch (InterruptedException e) {
            System.out.printf("Thread %s: Interrupted\n",name);
            return;
        }
        System.out.printf("%s: End\n",name);
    }


    // 模拟耗时查询
    private void doTask() throws InterruptedException {
        int value=(int)(Math.random()*10);
        System.out.printf("%s Sleeping %d\n",Thread.currentThread().getName(),value);
        TimeUnit.SECONDS.sleep(value);
    }




}
